<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryGoods.query" class="input-with-select" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4"> <el-button type="primary" @click="goAdd"> 添加商品 </el-button></el-col>
      </el-row>
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="95"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="70"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="140">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130">
          <template slot-scope="scope">
            <el-button size="mini" @click="editGoods(scope.$index, scope.row)" type="primary" icon="el-icon-edit"></el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteGoods(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryGoods.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryGoods.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total" background> </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Params',
  data() {
    return {
      goodsList: [],
      queryGoods: {
        query: '',
        pagenum: 1, //当前页码
        pagesize: 10 //当前一页有几条数据
      },
      total: 0
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 获取所有数据  &   搜索商品
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', { params: this.queryGoods })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      console.log(res)
      this.total = res.data.total
      this.goodsList = res.data.goods
    },
    //一页多少条数据
    handleSizeChange(val) {
      this.queryGoods.pagesize = val
      this.getGoodsList()
    },
    //当前在第几页
    handleCurrentChange(val) {
      this.queryGoods.pagenum = val
      this.getGoodsList()
    },
    async deleteGoods(row) {
      const res = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      if (res == 'confirm') {
        const { data: res } = await this.$http.delete(`goods/${row.goods_id}`)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.getGoodsList()
        this.$message.success('删除成功!')
      }
    },
    goAdd() {
      this.$router.push('/home/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  font-size: 10px;
}
</style>
